{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Structuring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sleep</th>\n",
       "      <th>Wake</th>\n",
       "      <th>Sleep_time</th>\n",
       "      <th>Wake_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-12-01</th>\n",
       "      <td>16</td>\n",
       "      <td>1</td>\n",
       "      <td>2017-12-01 22:16:00</td>\n",
       "      <td>2017-12-01 06:01:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-02</th>\n",
       "      <td>19</td>\n",
       "      <td>-10</td>\n",
       "      <td>2017-12-02 22:19:00</td>\n",
       "      <td>2017-12-02 05:50:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-03</th>\n",
       "      <td>-45</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-12-03 21:15:00</td>\n",
       "      <td>2017-12-03 06:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-04</th>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>2017-12-04 22:08:00</td>\n",
       "      <td>2017-12-04 06:01:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-05</th>\n",
       "      <td>-23</td>\n",
       "      <td>-16</td>\n",
       "      <td>2017-12-05 21:37:00</td>\n",
       "      <td>2017-12-05 05:44:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Sleep  Wake          Sleep_time           Wake_time\n",
       "2017-12-01     16     1 2017-12-01 22:16:00 2017-12-01 06:01:00\n",
       "2017-12-02     19   -10 2017-12-02 22:19:00 2017-12-02 05:50:00\n",
       "2017-12-03    -45     0 2017-12-03 21:15:00 2017-12-03 06:00:00\n",
       "2017-12-04      8     1 2017-12-04 22:08:00 2017-12-04 06:01:00\n",
       "2017-12-05    -23   -16 2017-12-05 21:37:00 2017-12-05 05:44:00"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = pd.read_csv('data/sleep_wake.csv')\n",
    "data.set_index('Date', inplace=True, drop=True)\n",
    "data.index=data.index.to_datetime(dayfirst=False)\n",
    "data = data.ix[:, 0:2]\n",
    "\n",
    "data['Sleep_time'] = [entry[0] + pd.DateOffset(minutes=entry[1], hours=22) for entry in zip(data.index, data['Sleep'])]\n",
    "data['Wake_time'] = [entry[0] + pd.DateOffset(minutes=entry[1], hours=6) for entry in zip(data.index, data['Wake'])]\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a separate sleep dataframe\n",
    "sleep_index = pd.date_range(start=min(data.index), end=max(data.index), freq='60S')\n",
    "sleep_index = [time for time in sleep_index if (time.hour >= 21) & (time.hour <= 23)]\n",
    "sleep_data = pd.DataFrame(0, columns=['indicator'], index=sleep_index)\n",
    "\n",
    "# Set an index with an indicator for times when I am asleep\n",
    "# Indicator = 1 when I am asleep, else 0\n",
    "for time in sleep_data.index:\n",
    "    for sleep_time in data['Sleep_time']:\n",
    "        if time == sleep_time:\n",
    "            selected_date = time.date()\n",
    "            date_subset = [date for date in sleep_data.index if date.date() == selected_date]\n",
    "            asleep_times = [time for time in date_subset if time.time() >= sleep_time.time()]\n",
    "            sleep_data.ix[asleep_times, 'indicator'] = 1\n",
    "            \n",
    "# Create a separate wake dataframe\n",
    "wake_index=pd.date_range(start=min(data.index), end=max(data.index), freq='60S')\n",
    "wake_index = [time for time in wake_index if (time.hour <= 7) & (time.hour >= 5)]\n",
    "wake_data = pd.DataFrame(1, columns=['indicator'], index=wake_index)\n",
    "\n",
    "# Set an index with indicator for times when I am asleep\n",
    "# Indicator = 1 when I am asleep, else 0\n",
    "for time in wake_data.index:\n",
    "    for wake_time in data['Wake_time']:\n",
    "        if time == wake_time:\n",
    "            selected_date = time.date()\n",
    "            date_subset = [date for date in wake_data.index if date.date() == selected_date]\n",
    "            wake_times = [time for time in date_subset if time.time() >= wake_time.time()]\n",
    "            wake_data.ix[wake_times, 'indicator'] = 0\n",
    "    \n",
    "# Time offset column \n",
    "# For sleep, offset from 10:00 PM\n",
    "sleep_data['time_offset'] = [int((time.hour * 60 + time.minute) - (22 * 60)) for time in sleep_data.index]\n",
    "# For wake, offset from 6:00 AM\n",
    "wake_data['time_offset'] = [int((time.hour * 60 + time.minute) - (6 * 60)) for time in wake_data.index]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write Data to a csv "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "sleep_data.to_csv('data/sleep_data.csv')\n",
    "wake_data.to_csv('data/wake_data.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
